﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Models;
using Kaiser.Repositories;

namespace Kaiser.Services.Impl
{
    public class UserService : IUserService
    {
        private readonly IRepository _repository;

        public UserService(IRepository repository)
        {
            _repository = repository;//
        }

        public Models.User GetItem(int userID)
        {
            try
            {
                return _repository.GetUserItem(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.User GetItem(string username)
        {
            try
            {
                //return new User(0,"kaiser","刘俊勋","liu@f.com","123456", UserStatus.Normal,  new UserGroupBasic());
                return _repository.GetUserItem(username);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.User GetItem(string username, Models.UserStatus status)
        {
            try
            {
                return _repository.GetUserItem(username,status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsExists(string username)
        {
            try
            {
                return _repository.IsExists(username);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsExists(int userID)
        {
            try
            {
                return _repository.IsExists(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ValidateUser(int userID, string username)
        {
            try
            {
                return _repository.ValidateUser(userID, username);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataGrid<Models.UserBasic> GetList(Models.UserSearch criteria, Models.PagingInfo pageingInfo)
        {
            try
            {
                return _repository.GetUserBasicList(criteria, pageingInfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.ModelResult SaveEdit(Models.InputModels.UserInputEdit userInput)
        {
            try
            {
                return _repository.SaveEdit(userInput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.ModelResult SaveAdd(Models.InputModels.UserInputAdd userInput)
        {
            try
            {
                return _repository.SaveAdd(userInput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.ModelResult Save(Models.InputModels.UserInput userInput)
        {
            try
            {
                return _repository.Save(userInput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetPasswordByUserName(string username, string password)
        {
            try
            {
                _repository.SetPasswordByUserName(username, password);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool Remove(int userID)
        {
            try
            {
                return _repository.Remove(userID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ChangeStatus(int userID, Models.UserStatus status)
        {
            try
            {
                return _repository.ChangeStatus(userID, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool SignIn(Func<Models.User> getUser, Action<Models.User> afterSignIn)
        {
            try
            {
                return _repository.SignIn(getUser, afterSignIn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SignOut()
        {
            try
            {
                _repository.SignOut();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
